﻿using Common.Prism.Apps;
using NxChatQQ.Models;
using NxChatQQ.Models.Events;
using NxChatQQ.Views.Layouts;
using Prism.Commands;
using Prism.Events;
using Prism.Regions;
using System;
using System.Collections.ObjectModel;
using System.Windows.Input;

namespace NxChatQQ.ViewModels.Layouts
{
    public class RecentListLayoutViewModel : BaseViewModel
    {

        private int _RecentListIndex = -1;
        public int RecentListIndex
        {
            get { return _RecentListIndex; }
            set { SetProperty(ref _RecentListIndex, value); }
        }

        public ObservableCollection<Friend> RecentItems { get; set; }

        private readonly IRegionManager _regionManager;
        public readonly IEventAggregator _eventAggregator;
        public ICommand SelectionChangedCommand { get; set; }

        public RecentListLayoutViewModel(IEventAggregator eventAggregator, IRegionManager regionManager)
        {
            _regionManager = regionManager;
            _eventAggregator = eventAggregator;

            _regionManager.RegisterViewWithRegion("ChatRegionContent", typeof(ChatMessageLayout));

            SelectionChangedCommand = new DelegateCommand(SelectionChanged);
            RecentItems = new ObservableCollection<Friend>();


            RecentItems.Add(new Friend()
            {
                Name = "123444",
                LastMsgTime = DateTime.Now,
                Content = "DateTime",
                MsgNum = 1,

            });

            RecentItems.Add(new Friend()
            {
                Name = "123444",
                LastMsgTime = DateTime.Now,
                Content = "1231222222222222222222223333",
                MsgNum = 10,
            });

            RecentItems.Add(new Friend()
            {
                Name = "123444",
                LastMsgTime = DateTime.Now,
                Content = "Date222222222222222222222222222222222222222222222222222222222222Time"
            }); RecentItems.Add(new Friend()
            {
                Name = "123442222222222222222222222222222222222222222222222222224",
                LastMsgTime = DateTime.Now,
                Content = "DateTime"
            }); RecentItems.Add(new Friend()
            {
                Name = "123444",
                LastMsgTime = DateTime.Now,
                Content = "Date222222222222222222222222222222Time"
            }); RecentItems.Add(new Friend()
            {
                Name = "123444",
                LastMsgTime = DateTime.Now,
                Content = "DateTime"
            }); RecentItems.Add(new Friend()
            {
                Name = "1234444444444444444444444444444444444444444444",
                LastMsgTime = DateTime.Now,
                Content = "DateTime"
            }); RecentItems.Add(new Friend()
            {
                Name = "123444",
                LastMsgTime = DateTime.Now,
                Content = "DateTime"
            }); RecentItems.Add(new Friend()
            {
                Name = "123444",
                MsgNum = 2299,
                LastMsgTime = DateTime.Now,
                Content = "DateTime"
            }); RecentItems.Add(new Friend()
            {
                Name = "123444",
                MsgNum = 99,
                LastMsgTime = DateTime.Now,
                Content = "DateTime"
            });
        }

        private void SelectionChanged()
        {
            if (RecentListIndex >= 0)
            {
                var item = RecentItems[RecentListIndex];
                _eventAggregator.GetEvent<UpdateChatEvent>().Publish(item);
            }
        }
    }
}